package com.cpi.cornpp.instructionsforecasts.converter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructionsforecasts.dto.InstructionsForecastsDTO;
import com.cpi.cornpp.instructionsforecasts.exception.InstructionsForecastsException;
import com.cpi.cornpp.instructionsforecasts.view.InstructionsForecastsView;
import com.cpi.cornpp.plantversion.exception.PlantVersionException;
import com.cpi.cornpp.product.view.ProductView;
import com.cs.common.utils.NumberUtil;

/**
 * Instructions Forecasts Version Converter
 * @author Edgar Joao
 *
 */
public class InstructionsForecastsConverter {
	/**
	 * Instructions Forecasts Version Converter Views To DTOs
	 * @author Edgar Joao
	 * @param view
	 * @return
	 * @throws PlantVersionException
	 */
	public InstructionsForecastsDTO converterInstructionsForecastsViewToDto(InstructionsForecastsView view) throws InstructionsForecastsException {
		InstructionsForecastsDTO instructionsForecastsDTO = null;
		try{
			if(view != null){
				instructionsForecastsDTO = new InstructionsForecastsDTO();
				instructionsForecastsDTO.setPurchaseInstructionDetailId(view.getPurchaseInstructionDetailId());
				instructionsForecastsDTO.setInstructionForecastId(view.getInstructionForecastId());
				instructionsForecastsDTO.setCustomerId(view.getCustomerId());
				instructionsForecastsDTO.setItemId(view.getItemId());
				instructionsForecastsDTO.setMonthPeriodId(view.getMonthPeriodId());
				instructionsForecastsDTO.setForecastAdded(view.getForecastAdded());
				instructionsForecastsDTO.setYear(view.getYear());
				instructionsForecastsDTO.setMonth(view.getMonth());
				instructionsForecastsDTO.setContracted(NumberUtil.parseDouble(view.getContracted()));
				instructionsForecastsDTO.setCornContracted(NumberUtil.parseDouble(view.getCornContracted()));
				instructionsForecastsDTO.setNonContracted(NumberUtil.parseDouble(view.getNonContracted()));
				instructionsForecastsDTO.setCornNonContracted(NumberUtil.parseDouble(view.getCornNonContracted()));
				instructionsForecastsDTO.setActual(NumberUtil.parseDouble(view.getActual()));
				instructionsForecastsDTO.setFutures(NumberUtil.parseDouble(view.getFutures()));
			}
		}catch( Exception exception ){
			InstructionsForecastsException instructionsForecastsException=null;
			exception.printStackTrace(System.out);
			instructionsForecastsException= new InstructionsForecastsException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionsForecastsException;
		}		
		return instructionsForecastsDTO;
	}
	
	public List convertDTOsToViewsByPeriod(List instructionsForecastsDTOs, List productViews, int month, int year) throws InstructionsForecastsException {
		List instructionsForecatsViews = null;
		Iterator iterator = null;
		InstructionsForecastsView instructionsForecastsView = null;
		InstructionsForecastsDTO instructionsForecastsDTO = null;
		try{
			instructionsForecatsViews = new ArrayList(12);
			iterator = instructionsForecastsDTOs.iterator();
			while(iterator.hasNext()){
				instructionsForecastsDTO = (InstructionsForecastsDTO) iterator.next();
				if( productSelected(instructionsForecastsDTO, productViews) && month == instructionsForecastsDTO.getMonth() && year == instructionsForecastsDTO.getYear() ){
					instructionsForecastsView = converterInstructionsForecastsDtoToView(instructionsForecastsDTO);
					instructionsForecatsViews.add(instructionsForecastsView);
				}
			}			
		}catch(Exception exception){
			InstructionsForecastsException instructionsForecastsException=null;
			exception.printStackTrace(System.out);
			instructionsForecastsException = new InstructionsForecastsException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionsForecastsException;
		}		
		return instructionsForecatsViews;
	}
	
	/**
	 * 
	 * @param instructionsForecastsDTOs
	 * @return
	 * @throws InstructionsForecastsException
	 */
	public List convertDTOsToViews(List instructionsForecastsDTOs, List productViews) throws InstructionsForecastsException {
		List instructionsForecatsViews = null;
		Iterator iterator = null;
		Iterator iteratorMap = null;
		InstructionsForecastsView instructionsForecastsView = null;
		Map instructionsForecatsViewMap;
		InstructionsForecastsDTO instructionsForecastsDTO = null;
		InstructionsForecastsDTO instructionsForecastsDTOTemp = null;
		String key = null;
		try{
			instructionsForecatsViews = new ArrayList(12);
			instructionsForecatsViewMap = new HashMap();
			iterator = instructionsForecastsDTOs.iterator();
			while(iterator.hasNext()){
				instructionsForecastsDTO = (InstructionsForecastsDTO) iterator.next();
				//TODO que los productos seleccionados vengan de la base de datos
				if( productViews!=null && productSelected(instructionsForecastsDTO, productViews) ){
					key = instructionsForecastsDTO.getMonth() + "-"+instructionsForecastsDTO.getYear();
					instructionsForecastsDTOTemp = (InstructionsForecastsDTO)instructionsForecatsViewMap.get(key);
					if( instructionsForecastsDTOTemp  != null ){
						instructionsForecastsDTOTemp.setContracted(instructionsForecastsDTOTemp.getContracted() + instructionsForecastsDTO.getContracted());
						instructionsForecastsDTOTemp.setCornContracted(instructionsForecastsDTOTemp.getCornContracted() + instructionsForecastsDTO.getCornContracted());
						instructionsForecastsDTOTemp.setNonContracted(instructionsForecastsDTOTemp.getNonContracted() + instructionsForecastsDTO.getNonContracted());
						instructionsForecastsDTOTemp.setCornNonContracted(instructionsForecastsDTOTemp.getCornNonContracted() + instructionsForecastsDTO.getCornNonContracted());
						instructionsForecastsDTOTemp.setFutures(instructionsForecastsDTOTemp.getFutures() + instructionsForecastsDTO.getFutures());
						instructionsForecastsDTOTemp.setActual(instructionsForecastsDTOTemp.getActual() + instructionsForecastsDTO.getActual());
					}else{
						instructionsForecatsViewMap.put(key, instructionsForecastsDTO);
					}
				}
			}			
			iteratorMap = instructionsForecatsViewMap.values().iterator();
			while(iteratorMap.hasNext()){
				instructionsForecastsDTO = (InstructionsForecastsDTO) iteratorMap.next();
				instructionsForecastsView = converterInstructionsForecastsDtoToView(instructionsForecastsDTO);
				instructionsForecatsViews.add(instructionsForecastsView);
			}
			Collections.sort(instructionsForecatsViews,  new Comparator() { public int compare(Object o1, Object o2) { return ((InstructionsForecastsView)o1).getMonth() < ((InstructionsForecastsView)o2).getMonth() ? -1 : 1; } });
		}catch(Exception exception){
			InstructionsForecastsException instructionsForecastsException=null;
			exception.printStackTrace(System.out);
			instructionsForecastsException = new InstructionsForecastsException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionsForecastsException;
		}		
		return instructionsForecatsViews;
	}
	
	private boolean productSelected(InstructionsForecastsDTO instructionsForecastsDTO, List productViews) {
		boolean productSelect = false;
		Iterator iterator = productViews.iterator();
		ProductView productView = null;
		while(iterator.hasNext()){
			productView = (ProductView)iterator.next();
			if( productView.getProductId().equals(instructionsForecastsDTO.getItemId().trim()) ){
				productSelect = true;
				break;
			}
		}
		return productSelect;
	}
	/**
	 * Instructions ForecastsView Converter DTO to View
	 * @param dto
	 * @return
	 * @throws PlantVersionException
	 */
	public InstructionsForecastsView converterInstructionsForecastsDtoToView(InstructionsForecastsDTO dto) throws InstructionsForecastsException{
		InstructionsForecastsView instructionsForecastsView = null;
		try{
			if(dto != null){
				instructionsForecastsView = new InstructionsForecastsView();
				instructionsForecastsView.setPurchaseInstructionDetailId(dto.getPurchaseInstructionDetailId());
				instructionsForecastsView.setInstructionForecastId(dto.getInstructionForecastId());
				instructionsForecastsView.setCustomerId(dto.getCustomerId());
				instructionsForecastsView.setItemId(dto.getItemId());
				instructionsForecastsView.setMonthPeriodId(dto.getMonthPeriodId());
				instructionsForecastsView.setForecastAdded(dto.getForecastAdded());
				instructionsForecastsView.setYear(dto.getYear());
				instructionsForecastsView.setMonth(dto.getMonth());
				instructionsForecastsView.setContracted(NumberUtil.convertQuantity(dto.getContracted()));
				instructionsForecastsView.setCornContracted(NumberUtil.convertQuantity(dto.getCornContracted()));
				instructionsForecastsView.setNonContracted(NumberUtil.convertQuantity(dto.getNonContracted()));
				instructionsForecastsView.setCornNonContracted(NumberUtil.convertQuantity(dto.getCornNonContracted()));
				instructionsForecastsView.setActual(NumberUtil.convertQuantity(dto.getActual()));
				instructionsForecastsView.setFutures(NumberUtil.convertQuantity(dto.getFutures()));
				instructionsForecastsView.setFuturesFloored(NumberUtil.floorQuantity(dto.getFutures()));
			}			
		}catch( Exception exception ){
			InstructionsForecastsException instructionsForecastsException=null;
			exception.printStackTrace(System.out);
			instructionsForecastsException= new InstructionsForecastsException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionsForecastsException;
		}
		return instructionsForecastsView;
	}	
}
